﻿Public Class TextBox : Inherits TextBoxBase

	Dim m_MinLength As Integer
	Dim m_Casing As CharacterCasing

	Dim m_Mask As String
	Dim m_OrgMask As String

	Property Mask() As String
		Get
			Return m_OrgMask
		End Get
		Set(ByVal Value As String)
			m_OrgMask = Value
			If InStr(Value, "&") > 0 Then
				Value = Replace(Value, "&", "_")
				m_OrgMask = Value
			End If
			m_Mask = Value
			If IsEmpty Then Me.Value = ""
		End Set
	End Property

	Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
		MyBase.OnGotFocus(e)
		If Mid(Text, 2, 6) = "/_____" And Mid(Text, 1, 1) <> "_" Then Me.Select(2, 1)
	End Sub

	Public Overrides Property Value() As Object
		Get
			If m_Mask = "" Then Return If(AllowOnlyNumeric AndAlso Text = "", Nothing, Text)
			Dim TmpText As String = ""
			For i As Integer = 1 To Len(m_Mask)
				Dim Chr As String = Mid(m_Mask, i, 1)
				Select Case Chr
					Case "_"
						Chr = Mid(Text, i, 1)
						If Chr <> "_" Then TmpText &= Chr
				End Select
			Next
			Select Case SpecialField
				Case "isyerino"
					If InStr(Mask, "&") = 0 Then Return TmpText
					Return Mid(Text, 1, 1) & TmpText
				Case "donem"
					Return TmpText
				Case Else
					Return TmpText
			End Select
		End Get
		Set(ByVal Value As Object)
			If IsDBNull(Value) Then Value = ""
			Select Case m_Mask
				Case ""
					Text = Value
				Case Else
					If Value = "" Then
						Dim Temp As String = m_Mask
						If Temp = "_/_____" Then
							If Parent Is Nothing Then
								Mid(Temp, 1, 1) = Department.LoginBranch
							ElseIf Not Process Is Nothing Then
								Mid(Temp, 1, 1) = Process.Window.CurrentLocalDepartment
							Else
								Mid(Temp, 1, 1) = If(User.Department.ID = 0, "1", User.Department.ID)
							End If
						End If
						Text = Temp
						Exit Property
					End If
					Dim TmpText As String = ""
					For i As Integer = 1 To Len(m_OrgMask)
						Dim Chr As String = Mid(m_OrgMask, i, 1)
						Select Case Chr
							Case "_", "&"
								Dim InsChar As String = Mid(Value, 1, 1)
								If InsChar = "" Then InsChar = "_"
								TmpText &= InsChar
								Value = Mid(Value, 2)
							Case Else
								TmpText &= Chr
						End Select
					Next
					Text = TmpText
			End Select
		End Set
	End Property

	Property MinLength() As Integer
		Get
			Return m_MinLength
		End Get
		Set(ByVal Value As Integer)
			m_MinLength = Value
		End Set
	End Property

	Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		If m_Mask <> "" Then
			Select Case e.KeyCode
				Case Keys.Left
					FocusMaskChar(SelectionStart - 1, -1)
					e.Handled = True
				Case Keys.Right
					FocusMaskChar(SelectionStart + 1, 1)
					e.Handled = True
				Case Keys.Back
					FocusMaskChar(SelectionStart - 1, -1)
					SelectedText = "_"
					FocusMaskChar(SelectionStart - 1, 0)
					e.Handled = True
				Case Keys.Delete
					If SelectionStart = 0 And SelectionLength = Len(Mask) Then
						Value = ""
					Else
						FocusMaskChar(SelectionStart, 0)
						SelectedText = "_"
						FocusMaskChar(SelectionStart - 1, 0)
					End If
					e.Handled = True
			End Select
		End If
	End Sub

	Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
		If AllowOnlyNumeric Then
			If (e.KeyChar < "0" Or e.KeyChar > "9") And (e.KeyChar <> Chr(8)) Then
				e.Handled = True
			End If
		ElseIf Mask <> "" Then
			If e.KeyChar = Chr(8) Then
				e.Handled = True
			ElseIf InStr("0123456789", e.KeyChar) > 0 Then
				Dim SelStart As Integer = SelectionStart
				For i As Integer = SelectionStart + 1 To SelectionStart + SelectionLength + 1
					If Mid(Mask, i, 1) = "_" Then Mid(Text, i, 1) = "_"
				Next
				FocusMaskChar(SelStart, 1)
				If SelectedText <> "" Then
					SelectedText = e.KeyChar
				End If
				FocusMaskChar(SelectionStart, 1)
				e.Handled = True
			Else
				e.Handled = True
			End If
		End If
	End Sub

	Private Sub TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
		If SpecialField = "" Then Exit Sub
		FixSpecialField()
	End Sub

	Sub FixSpecialField()
		Select Case SpecialField
			Case "isyerino"
				If IsEmpty Then Exit Sub
				Text = Mid(Text, 1, 2) & Replace(Mid(Text, 3), "_", "").PadLeft(5, "0")
			Case "kimlik"
				If IsEmpty Then Exit Sub
				If Len(Text) < 6 Then Text = StrDup(6 - Len(Text), "0") & Text
			Case "donem"
				If IsEmpty Then Exit Sub
				Text = Mid(Text, 1, 5) & Replace(Mid(Text, 6), "_", "").PadLeft(2, "0")
		End Select
	End Sub

	Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
		Static Updating As Boolean
		If Updating Then Exit Sub
		With TextInfo
			.Text = Text
			.SelectionStart = SelectionStart
			.SelectionLength = SelectionLength
		End With
		If Casing <> SSD.CharacterCasing.Normal Then
			Dim NewText As String = Text
			If Not NewText = Nothing Then
				Select Case Casing
					Case SSD.CharacterCasing.Lower
						NewText = NewText.Replace("I", "ı")
						NewText = NewText.Replace("İ", "i")
						NewText = NewText.ToLower
					Case SSD.CharacterCasing.Upper
						NewText = NewText.Replace("i", "İ")
						NewText = NewText.Replace("ı", "I")
						NewText = NewText.ToUpper
				End Select
			End If
			If NewText <> Text Then
				With TextInfo
					Updating = True
					Text = NewText
					SelectionStart = .SelectionStart
					SelectionLength = .SelectionLength
					Updating = False
				End With
			End If
		End If
		RaiseChangeEvent()
	End Sub

	Private Sub FocusMaskChar(ByVal Index, ByVal Offset)
		Do
			If Index < 0 Then Exit Sub
			If Index > Len(Mask) Then Exit Sub
			Select Case Mid(Mask, Index + 1, 1)
				Case "_"
					SelectionStart = Index
					Exit Do
			End Select
			Index = Index + Offset
		Loop
		SelectionLength = 1
	End Sub

	Property Casing() As CharacterCasing
		Get
			Return m_Casing
		End Get
		Set(ByVal Value As CharacterCasing)
			m_Casing = Value
		End Set
	End Property

	ReadOnly Property BreaksMinRule() As Boolean
		Get
			Return Len(Value) < MinLength
		End Get
	End Property

	ReadOnly Property BreaksMaxRule() As Boolean
		Get
			If Mask <> "" Then Return False
			If MaxLength = 0 Then Return False
			Return Len(Value) > MaxLength
		End Get
	End Property

	Public Overrides ReadOnly Property IsEmpty() As Boolean
		Get
			Select Case SpecialField
				Case "isyerino"
					Return Len(Value) <= 1
			End Select
			Return Trim(IsNull(Value, "")) = ""
		End Get
	End Property

	Public Overrides Function Errors() As String
		If IsRequired AndAlso IsEmpty Then Return Caption & " alanı boş olamaz."
		If Not IsEmpty Then
			If BreaksMinRule Then Return Caption & " alanı en az " & MinLength & " uzunluğunda olmalıdır."
			If BreaksMaxRule Then Return Caption & " alanı en çok " & MaxLength & " uzunluğunda olmalıdır."
		End If
		Return ""
	End Function

End Class